package com.dexter.year2023.charpter4_stack.level1;

import java.util.LinkedList;

/**
 * 用Java自带的LinkedList实现Stack
 *
 * @author Dexter
 */
public class Demo4LinkedListStack<T> {
    private LinkedList<T> l1 = new LinkedList<>();

    // 栈空
    public boolean isEmpty() {
        return l1.isEmpty();
    }

    // 栈顶
    public T peek() {
        T t = null;
        if (!l1.isEmpty()) {
            // 取第一个元素
            t = l1.getFirst();
        }
        return t;
    }

    // 入栈
    public void push(T t) {
        l1.addFirst(t);
    }

    // 出栈
    public T pop() {
        return l1.removeFirst();
    }

    public static void main(String[] args) {
        // 测试用例
        Demo4LinkedListStack<String> stack = new Demo4LinkedListStack<>();
        System.out.println(stack.peek());
        System.out.println(stack.isEmpty());
        stack.push("Stephen Curry");
        stack.push("Klay Thompson");
        stack.push("Draymond Green");
        System.out.println(stack.pop());  // Draymond Green
        System.out.println(stack.isEmpty());
        System.out.println(stack.peek()); // Klay Thompson
    }
}
